import pytest
import allure
import requests

# 使用类的方式，关联接口返回
# 第一种，使用类属性
"""
@allure.feature("CRMEB项目")
class TestCRMEB:
    base_url = "http://8.140.193.160:8899"
    list1 = []
    @allure.story("登录")
    def test_login(self):
        response = requests.post(self.base_url + "/adminapi/login",json={"account":"admin","pwd":"920509","imgcode":"a"})
        assert response.json()['msg'] == 'ok'
        assert response.status_code == 200
        self.list1.append(response.json()['data']['token'])
    # @pytest.mark.skip
    @allure.story("客服话术查询")
    def test_search_cumser(self):
        res = requests.get(self.base_url + "/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=",
                           headers={"Authori-zation":"Bearer "+ str(self.list1[0]) })
        assert res.status_code == 200
"""
# 第二种 ，使用fixture的方式，每条接口都先登录，拿到token后，再给后面做关联使用
"""
@allure.feature("CRMEB项目")
class TestCRMEB:
    base_url = "http://8.140.193.160:8899"
    @pytest.fixture(scope='function',autouse=True)
    def login(self):
        response = requests.post(self.base_url + "/adminapi/login",json={"account":"admin","pwd":"920509","imgcode":"a"})
        assert response.json()['msg'] == 'ok'
        assert response.status_code == 200
        self.token = response.json()['data']['token']
    # @pytest.mark.skip
    @allure.story("客服话术查询")
    def test_search_cumser(self):
        res = requests.get(self.base_url + "/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=",
                           headers={"Authori-zation":"Bearer "+ str(self.token) })
        assert res.status_code == 200
"""
# 第三种 ，将token写到文件里，每个测试类，只登录一次，后续读
@allure.feature("CRMEB项目")
class TestCRMEB:
    base_url = "http://8.140.193.160:8899"
    @pytest.fixture()
    def login(self):
        response = requests.post(self.base_url + "/adminapi/login",json={"account":"admin","pwd":"920509","imgcode":"a"})
        assert response.json()['msg'] == 'ok'
        assert response.status_code == 200
        # self.token = response.json()['data']['token']
        with open('token.txt','w',encoding='utf-8') as f:
            f.write(response.json()['data']['token'])
    # @pytest.mark.skip
    @allure.story("客服话术查询")
    def test_search_cumser(self,login):
        with open("token.txt",'r',encoding='utf-8') as f:
            result = f.read()
        res = requests.get(self.base_url + "/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=",
                           headers={"Authori-zation":"Bearer "+ str(result)})
        assert res.status_code == 200
    @allure.story("用户分组查询")
    def test_uear_search(self):
        with open("token.txt",'r',encoding='utf-8') as f:
            result = f.read()
        res = requests.get(self.base_url + "/adminapi/app/wechat/speechcraft?page=1&limit=15",
                     headers={"Authori-zation": "Bearer " + str(result)})
        assert res.status_code == 200












